local skynet = require "skynet"
local filelog = require "filelog"
local msghelper = require "tablehelper"
local logicmng = require "logicmng"
local timer = require "timer"
local base = require "base"
local tabletool = require "tabletool"
local playerdatadao = require "playerdatadao"
local msgproxy = require "msgproxy"
require "enum"

local filename = "tabletimer.lua"

local TableTimer = {}

function TableTimer.process(session, source, event, ...)
	local f = TableTimer[event] 
	if f == nil then
		filelog.sys_error(filename.." TableTimer.process invalid event:"..event)
		return nil
	end
	f(...)	 
end

function TableTimer.one_game_start(timerid, request)
	local server = msghelper:get_server()
	local table_data = server.table_data
	if timerid ~= table_data.timer_id then
		return
	end

	if table_data.state ~= ENNTableState.TABLE_STATE_ONE_WAIT_GAME_START then
		return
	end

	table_data.timer_id = -1

	table_data.state = ENNTableState.TABLE_STATE_DEAL_FOUR_CARDS
	local roomgamelogic = msghelper:get_game_logic()
	roomgamelogic.run(table_data.gamelogic)	
end

function TableTimer.one_game_end(timerid, request)
	local server = msghelper:get_server()
	local table_data = server.table_data
	if timerid ~= table_data.timer_id then
		return
	end

	if table_data.state ~= ENNTableState.TABLE_STATE_WAIT_GAME_END then
		return
	end

	table_data.timer_id = -1

	table_data.state = ENNTableState.TABLE_STATE_ONE_GAME_END
	local roomgamelogic = msghelper:get_game_logic()
	roomgamelogic.run(table_data.gamelogic)	
end

function TableTimer.deal_four_cards(timerid, request)
	local server = msghelper:get_server()
	local table_data = server.table_data
	if timerid ~= table_data.timer_id then
		return
	end

	if table_data.state ~= ENNTableState.TABLE_STATE_WAIT_DEAL_FOUR_CARDS_END then
		return
	end

	table_data.timer_id = -1

	table_data.state = ENNTableState.TABLE_STATE_QIANGZHUANG
	local roomgamelogic = msghelper:get_game_logic()
	roomgamelogic.run(table_data.gamelogic)
end

function TableTimer.qiang_zhuang(timerid, request)
	local server = msghelper:get_server()
	local table_data = server.table_data
	if timerid ~= table_data.timer_id then
		return
	end

	if table_data.state ~= ENNTableState.TABLE_STATE_WAIT_QIANGZHUANG then
		return
	end

	table_data.timer_id = -1
	table_data.action_player_list = nil
	table_data.state = ENNTableState.TABLE_STATE_DING_ZHUANG
	local roomgamelogic = msghelper:get_game_logic()
	roomgamelogic.run(table_data.gamelogic)
end

function TableTimer.ding_zhuang(timerid, request)
	local server = msghelper:get_server()
	local table_data = server.table_data
	if timerid ~= table_data.timer_id then
		return
	end

	if table_data.state ~= ENNTableState.TABLE_STATE_WAIT_DING_ZHUANG then
		return
	end

	table_data.timer_id = -1

	table_data.state = ENNTableState.TABLE_STATE_JIAOFEN
	local roomgamelogic = msghelper:get_game_logic()
	roomgamelogic.run(table_data.gamelogic)	
end

function TableTimer.jiao_fen(timerid, request)
	local server = msghelper:get_server()
	local table_data = server.table_data
	if timerid ~= table_data.timer_id then
		return
	end

	if table_data.state ~= ENNTableState.TABLE_STATE_WAIT_JIAOFEN then
		return
	end

	--通知client操作
	local roomgamelogic = msghelper:get_game_logic()
	local noticemsg = {}
	for _, seat in ipairs(table_data.seats) do
		if roomgamelogic.is_ingame(table_data.gamelogic, seat) 
			and seat.call_score <= 0 
			and seat.bankertag ~= ENNBankerType.NNBANKER_TYPE_BANKER then
			seat.call_score = 1			
			noticemsg.rid = seat.rid
			noticemsg.roomsvr_seat_index = seat.index
			noticemsg.action_type = ENNActionType.ACTION_TYPE_JIAOFEN
			noticemsg.call_score = seat.call_score
			msghelper:sendmsg_to_alltableplayer("NNDoactionResultNtc", noticemsg)
		end
	end

	table_data.timer_id = -1
	table_data.curr_actionplayer_rid = 0
	table_data.action_player_list = nil
	table_data.curr_actionplayer_cards = nil

	table_data.state = ENNTableState.TABLE_STATE_DEAL_FIFTH_CARDS
	roomgamelogic.run(table_data.gamelogic)	
end

function TableTimer.ping_niu(timerid, request)
	local server = msghelper:get_server()
	local table_data = server.table_data
	if timerid ~= table_data.timer_id then
		return
	end

	if table_data.state ~= ENNTableState.TABLE_STATE_WAIT_PINGNIU then
		return
	end

	--通知client操作
	local roomgamelogic = msghelper:get_game_logic()
	local player_action		
	local noticemsg = {}
	for _, seat in ipairs(table_data.seats) do
		if roomgamelogic.is_ingame(table_data.gamelogic, seat) then
			player_action = table_data.action_player_list[seat.rid]
			if player_action.action_state < 1 then
				noticemsg.rid = seat.rid
				noticemsg.roomsvr_seat_index = seat.index
				noticemsg.action_type = ENNActionType.ACTION_TYPE_WUPINGNIU
				for _, action_type in ipairs(player_action.action_types) do
					if action_type ~= ENNActionType.ACTION_TYPE_WUPINGNIU then
						noticemsg.action_type = action_type
						break
					end
				end 
				msghelper:sendmsg_to_alltableplayer("NNDoactionResultNtc", noticemsg)
			end
		end
	end

	table_data.timer_id = -1
	table_data.action_player_list = nil
	table_data.curr_actionplayer_rid = 0
	table_data.curr_actionplayer_cards = nil

	table_data.state = ENNTableState.TABLE_STATE_DOUNIU
	local roomgamelogic = msghelper:get_game_logic()
	roomgamelogic.run(table_data.gamelogic)	
end

function TableTimer.applydisband(timerid, request)
	local server = msghelper:get_server()
	local table_data = server.table_data
	if timerid ~= table_data.disband_table_timer_id then 
		return
	end
	table_data.disband_table_timer_id = -1
	local roomtablelogic = logicmng.get_logicbyname("roomtablelogic")
	if roomtablelogic.is_gameend(table_data) then
		return
	end

	if table_data.gamerecords ~= nil then
	table_data.state = ENNTableState.TABLE_STATE_DISBAND_GAME
	local roomgamelogic = msghelper:get_game_logic()
	roomgamelogic.run(table_data.gamelogic)	
	else
		--检查桌子当前是否能够删除
		if table_data.delete_table_timer_id > 0 then
			timer.cleartimer(table_data.delete_table_timer_id)
			table_data.delete_table_timer_id = -1
		end
		table_data.delete_table_timer_id = timer.settimer(400*100, "delete_table") 	
	end
end

function TableTimer.doready(timerid, request)
	local server = msghelper:get_server()
	local table_data = server.table_data
	local seat = table_data.seats[request.roomsvr_seat_index]	
	if seat.rid ~= request.rid then
		return
	end
	if timerid ~= seat.ready_timer_id then 
		return
	end
	if seat.ready_timer_id > 0 then
		timer.cleartimer(seat.ready_timer_id)
		seat.ready_timer_id = -1
	end
	--将玩家站起
	seat.ready_to_time = 0
	local roomtablelogic = logicmng.get_logicbyname("roomtablelogic")
	roomtablelogic.passive_standuptable(table_data, request, seat, EStandupReason.STANDUP_REASON_READYTIMEOUT_STANDUP)	
end

function TableTimer.doaction(timerid, request)
	local server = msghelper:get_server()
	local table_data = server.table_data
	if table_data.timer_id ~= timerid then
		return
	end
 	table_data.timer_id =-1
	table_data.state = ENNTableState.TABLE_STATE_CONTINUE
	local roomgamelogic = msghelper:get_game_logic()
	roomgamelogic.run(table_data.gamelogic)
end

function TableTimer.game_ready_start(timerid, request)
	local server = msghelper:get_server()
	local table_data = server.table_data
	if table_data.timer_id ~= timerid then
		return
	end
 	table_data.timer_id =-1
 	if table_data.state ~= ENNTableState.TABLE_STATE_WAIT_ALL_READY then
 		return
 	end
 	local roomgamelogic = msghelper:get_game_logic()

 	for _, seat in ipairs(table_data.seats) do
 		if roomgamelogic.is_ingame(table_data.gamelogic, seat) then
 			seat.state = ENNSeatState.SEAT_STATE_WAIT_START 			
 		end
 	end 	
 	table_data.state = ENNTableState.TABLE_STATE_ONE_GAME_START

	roomgamelogic.run(table_data.gamelogic)
end

function TableTimer.delete_table(timerid, request)
    local server = msghelper:get_server()    
    local table_data = server.table_data

    if request ~= nil and request.create_user_rid ~= nil then
    	if table_data.conf.isaapay == nil or table_data.conf.isaapay == 0 then
    		local status, onlineinfo = playerdatadao.query_player_online(request.create_user_rid)
	    	if status == nil then
	    		filelog.sys_obj("passive_disband", tostring(request.create_user_rid), table_data.conf)
	    	elseif onlineinfo.gatesvr_id ~= "" and onlineinfo.gatesvr_service_address ~= -1 then
	    		msgproxy.sendrpc_noticemsgto_gatesvrd(onlineinfo.gatesvr_id, onlineinfo.gatesvr_service_address, "passive_disband",
	    		 {rid=request.create_user_rid, retain_to_num = table_data.conf.retain_to_num,
	    		  gemstonenum = table_data.conf.gemstonenum, create_table_id = table_data.conf.create_table_id})
	    	else
	    		local money
	    		status, money = playerdatadao.query_player_money(request.create_user_rid)
	    		money.gemstone = money.gemstone + table_data.conf.gemstonenum
	    		playerdatadao.save_player_money("update", request.create_user_rid, money)
	    	end
    	end
    end

    if table_data.delete_table_timer_id == timerid then
        table_data.delete_table_timer_id = -1
        msghelper:event_process("lua", "cmd", "delete","timer")
    end 
end

----用于客户端展示牌局游戏结果界面定时器时间到
function TableTimer.onegameend(timerid, request)
	local server = msghelper:get_server()
	local table_data = server.table_data

	if table_data.timer_id ~= timerid then
		return
	end
	
	table_data.timer_id = -1
	
	local roomtablelogic = logicmng.get_logicbyname("roomtablelogic")
	--------切换房间状态到开启下一局游戏的状态
	table_data.state = ENNTableState.TABLE_STATE_ONE_GAME_REAL_END
	local roomgamelogic = msghelper:get_game_logic()
	roomgamelogic.run(table_data.gamelogic)
end


return TableTimer